package com.wzw.springboot.principle.mvc;

import com.wzw.springboot.principle.lifecycle.bean.Bean1;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.WebAsyncTask;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Wangzhiwen
 */
@RestController
@RequestMapping("/demo")
public class DemoController {

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    {
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setKeepAliveSeconds(60);
        executor.setQueueCapacity(100);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
    }

    @PostMapping
    public Map<String, String> demo(@RequestBody Map<String, String> request) {
        Map<String, String> map = new HashMap<>(3);
        map.put("1","1");
        map.put("2","2");
        map.put("3","4");
        return map;
    }

    @PostMapping("/async")
    public WebAsyncTask<String> async(@RequestBody Map<String, String> request){
        return new WebAsyncTask<String>(10L, executor, () -> "async");
    }


    @PostMapping("/list")
    public void list(@RequestBody List<Bean1> body){
        System.out.println(body);
    }

    @PostMapping("/list1")
    public void list(@RequestBody Req body){
        System.out.println(body);
    }

}
